package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CmsPageService {

    @Autowired
    private CmsPageRepository cmsPageRepository;

    // 分页查询页面(根据页面别名,站点ID,页面ID)
    public QueryResponseResult findList( int page,int size, QueryPageRequest queryRequestPage) {
        if (queryRequestPage == null){
            queryRequestPage = new QueryPageRequest();
        }
        //设置条件对象
        CmsPage cmsPage = new CmsPage();
        if (StringUtils.isNotEmpty(queryRequestPage.getSiteId())){
            // 设置站点ID
            cmsPage.setSiteId(queryRequestPage.getSiteId());
        }
        if (StringUtils.isNotEmpty(queryRequestPage.getPageId())){
            // 设置页面ID
            cmsPage.setPageId(queryRequestPage.getPageId());
        }
        if (StringUtils.isNotEmpty(queryRequestPage.getPageAliase())){
            // 设置别名
            cmsPage.setPageAliase(queryRequestPage.getPageAliase());
        }
        // 别名模糊匹配
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("pageAliase",ExampleMatcher.GenericPropertyMatchers.contains());
        Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);
        if (page <= 0){
            page = 1;
        }
        if (size <= 0){
            size = 10;
        }
        Pageable pageable = PageRequest.of(page-1,size);
        Page<CmsPage> cmsPagePage = cmsPageRepository.findAll(example,pageable);//自定义条件查询
        QueryResult queryResult = new QueryResult();
        queryResult.setList(cmsPagePage.getContent());
        queryResult.setTotal(cmsPagePage.getTotalElements());
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }

    // 新增页面
    public CmsPageResult addCmsPage(CmsPage cmsPage){
        //根据页面名称,站点ID,webpath查询页面
        CmsPage cmsPage1 = cmsPageRepository.findBySiteIdAndPageNameAndPageWebPath(cmsPage.getSiteId(), cmsPage.getPageName(), cmsPage.getPageWebPath());
        if (cmsPage1 == null){
            // cmsPage1不存在才新增
            cmsPage.setPageId(null);
            cmsPageRepository.save(cmsPage);
            return new CmsPageResult(CommonCode.SUCCESS,cmsPage);
        }
        return new CmsPageResult(CommonCode.FAIL,null);
    }

    // 根据ID查页面信息
    public CmsPage findById(String id) {
        Optional<CmsPage> optional = cmsPageRepository.findById(id);
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    // 修改页面
    public CmsPageResult update(String id, CmsPage cmsPage) {
        //  根据ID查页面信息
        CmsPage one = this.findById(id);
        if (one != null){
            // 修改页面信息
            one.setPageName(cmsPage.getPageName());
            one.setPageAliase(cmsPage.getPageAliase());
            one.setPageWebPath(cmsPage.getPageWebPath());
            cmsPageRepository.save(one);
            return new CmsPageResult(CommonCode.SUCCESS, one);
        }
        // 修改失败
        return new CmsPageResult(CommonCode.FAIL,null);
    }

    // 删除页面
    public ResponseResult delete(String id) {
        //先查一下
        Optional<CmsPage> optional = cmsPageRepository.findById(id);
        if (optional.isPresent()){
            cmsPageRepository.deleteById(id);
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }
}
